﻿using System;
using System.Linq.Expressions;
using VirtualTournaments.DataAccess.SqlServer.DataMappers;

namespace VirtualTournaments.DataAccess.SqlServer.DataProxies
{
	internal sealed class BetDataProxy : Business.Bets.Bet, IDataProxy<SqlServer.Bet>, Identifiable<SqlServer.Bet>
	{
		internal BetDataProxy(SqlServer.Bet dataObject)
			: base(null, dataObject.WageredMoney, Convert.ToDecimal(dataObject.Odds))
		{
			InnerDataObject = dataObject;
			MapProperties();
		}

		#region Override properties

		internal bool BettorHasBeenRead { get; set; }
		public override Business.Users.User Bettor
		{
			get
			{
				if (!BettorHasBeenRead)
				{
					BettorHasBeenRead = true;
					if (base.Bettor == null)
					{
						base.Bettor = MapBettor();
					}
				}
				return base.Bettor;
			}
		}

		#endregion

		#region Mapping methods

		private void MapProperties()
		{
		}

		private Business.Users.User MapBettor()
		{
			return UsersDataMapper.Instance.DataObject2BusinessObject(InnerDataObject.BettorUser);
		}

		#endregion

		#region IDataProxy<Bet> Members

		public SqlServer.Bet InnerDataObject { get; set; }

		#endregion

		#region Identifiable<Bet> Members

		public Expression<Func<SqlServer.Bet, bool>> GetUniqueWhereExpression()
		{
			return bet => bet.BetId == InnerDataObject.BetId;
		}

		#endregion
	}
}
